package com.example.tian.securityinspection.context;

import android.app.Application;
import android.widget.Toast;

import com.example.tian.securityinspection.controller.MainController;
import com.example.tian.securityinspection.module.ApplicationModule;
import com.example.tian.securityinspection.module.library.ContextProvider;
import com.example.tian.securityinspection.module.library.InjectorModule;
import com.example.tian.securityinspection.module.qualifiers.ShareDiretory;
import com.example.tian.securityinspection.network.GsonHelper;
import com.example.tian.securityinspection.util.Injector;
import com.example.tian.securityinspection.util.PreferenceUtil;
import com.example.tian.securityinspection.util.ToastUtil;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;


import javax.inject.Inject;

import dagger.ObjectGraph;

public class AppContext extends Application implements Injector {
    private static AppContext mInstance;

    private ObjectGraph mObjectGraph;

    private RefWatcher mRefWatcher;


    //注入主控制器
    @Inject
    MainController mMainController;

    @Inject
    @ShareDiretory
    File mShareLocation;

    public static AppContext getContext(){
        return mInstance;
    }


    public RefWatcher getRefWatcher() {
        return mRefWatcher;
    }

    public MainController getMainController() {
        return mMainController;
    }

    //全局初始化操作
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        //吐司初始化
        ToastUtil.init(this);

        //本地存储工具类初始化
        PreferenceUtil.init(this, GsonHelper.builderGson());

        //日志打印器初始化
        Logger.init(getPackageName()).setLogLevel(AppConfig.DEBUG ? LogLevel.FULL : LogLevel.NONE);

        //bugley初始化(热更新)
        /*暂时未申请*/
        CrashReport.initCrashReport(this,"" ,false);

        //LeakCanary
        mRefWatcher = LeakCanary.install(this);

        //依赖注解初始化
        mObjectGraph = ObjectGraph.create(
                new ApplicationModule(),
                new ContextProvider(this),
                new InjectorModule(this)
        );
        mObjectGraph.inject(this);
    }

    @Override
    public void inject(Object object) {
        mObjectGraph.inject(object);
    }
}
